// 2013-10-13上午11:18:03  net.betou.trade.role.setting.ProxyMobileFinderConfig.java

package net.betou.trade.role.setting;

import net.betou.trade.entity.main.AgentAndAdmins;

/**
 * 
 * @author 叶装装
 */
public class ProxyMobileFinderConfig {
	
	private AgentAndAdmins admin;
	private int deep = 10000;
	private String viewRoles;
	
	public ProxyMobileFinderConfig(  AgentAndAdmins admin, String viewRoles ) {
		this.admin = admin;
		this.viewRoles = viewRoles;
	}

	protected String getBegin( ) {
		return "id in(";
	}

	protected String getEnd( ) {
		return " ) and is_system=0 and just_admin=0 and is_agency=0";
	}
	
	public String getDeepSQL(  ){
		Integer adminId = admin.getId( );
		return getBegin( ) + this.getSQL( deep, adminId, viewRoles ) + getEnd( );
	}
	
	private String getSQL( Integer deep, Integer adminId, String viewRoles ){
		StringBuffer sqlBuffer = new StringBuffer( );
		sqlBuffer.append( "select t.admin_id from trade_admin_role t, trade_admin a where t.admin_id = a.id and a.deep <=" + deep );
		sqlBuffer.append( " and find_in_set(" + adminId + ",a.top_intermedia) " );
		sqlBuffer.append( " and " + viewRoles );
		
		sqlBuffer.append( "  group by t.admin_id" );
		
		return sqlBuffer.toString( );
	}
	
	
}
